import { getDetailAPI, editAPI } from '@/apis/card'
//@ts-ignore
import DateUtils from '@/utils/dateUtils';
/**
 * 约定：
 * data,methods ,computed里面变量和方法名统统以edit开头
 */
export default {
  data() {
    return {
      editDialogFormData: {
        type: 'edit',
        id: null,
        dialogVisible: false,
        title: '编辑月卡',

        form: {
          personName: '', // 车主姓名
          phoneNumber: '', // 联系方式
          carNumber: '', // 车牌号码
          carBrand: '', // 车辆品牌
          payTime: [],
          paymentAmount: '',
          paymentMethod: '',
        }
      }
    }
  },
  beforeCreate() {
    this.editCopyDetailForm = {} //记录后端返回的数据，重置的时候用到
  },
  methods: {
    async editGetDetail(id) {
      this.editDialogFormData.id = id
      const r = await getDetailAPI(id)

      let { form } = this.editDialogFormData
      Object.keys(form).forEach(key => {
        if (key !== 'payTime') form[key] = r.data[key]
        else form.payTime = [r.data.cardStartDate, r.data.cardEndDate]
      })
      this.editCopyDetailForm = JSON.parse(JSON.stringify({ ...r.data, ...form })) //备份一下原数据和处理好的数据。页面展示只要处理好的数据，但是最后的编辑接口需要所有的数据
      this.editDialogFormData.dialogVisible = true
    },
    editResetFrom() {
      const editCopyDetailForm = this.editCopyDetailForm
      const form = this.editDialogFormData.form
      Object.keys(form).forEach(key => form[key] = editCopyDetailForm[key])

    },
    async editComfirmForm() {
      const { payTime, ...params } = this.editDialogFormData.form
      params.cardStartDate = DateUtils.dataFormat(payTime[0])
      params.cardEndDate = DateUtils.dataFormat(payTime[1])
      params.carInfoId=this.editCopyDetailForm.carInfoId
      params.rechargeId=this.editCopyDetailForm.rechargeId
      console.log('params',params)
      
      try {
        const r = await editAPI(params)
        this.$message.success('编辑成功')
        this.fetchTableData()
      } catch (error) {
        this.$message.error(error.msg || '编辑失败')

      }

    }
  },
  mounted() {
  }
}